﻿using BCCommon;
using BCData.MakeStaticPagingComponent.WebsiteTemplate;
using BCData.MakeStaticPagingComponent.WebsiteTemplateAttachment;
using BCDto.MakeStaticPagingComponent.WebsiteTemplateAttachment;
using BCDto.Sim.Admin.Admin;
using BCEntity.Common.EntityNameConst;
using BCEntity.MakeStaticPagingComponent.WebsiteTemplateAttachment;
using BCService.Utils;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BCService.MakeStaticPagingComponent.WebsiteTemplateAttachmentService
{
    public class WebsiteTemplateAttachmentService : IWebsiteTemplateAttachmentService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IConfiguration configuration;
        private readonly IRedisService redisService;
        private readonly IWebsiteTemplateData websiteTemplateData;
        private readonly IWebsiteTemplateAttachmentData websiteTemplateAttachmentData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteTemplateAttachmentEntity);

        public WebsiteTemplateAttachmentService(IDatabaseContext databaseContext,
            IConfiguration configuration,
            IRedisService redisService,
            IWebsiteTemplateData websiteTemplateData,
            IWebsiteTemplateAttachmentData websiteTemplateAttachmentData)
        {
            this.databaseContext = databaseContext;
            this.configuration = configuration;
            this.redisService = redisService;
            this.websiteTemplateData = websiteTemplateData;
            this.websiteTemplateAttachmentData = websiteTemplateAttachmentData;
        }

        #region 检查网站模板附件是否存在
        public bool Exists(int websiteTemplateAttachmentId)
        {
            return this.websiteTemplateAttachmentData.Exists(websiteTemplateAttachmentId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加网站模板附件
        public WebsiteTemplateAttachmentDto Add(WebsiteTemplateAttachmentRequestDto requestDto,AdminDto adminDto)
        {
            if (!this.websiteTemplateData.CheckWebsiteTemplateExists(requestDto.WebsiteTemplateId))
            {
                throw new ArgumentException("此网站模板不存在!");
            }
            var websiteTemplateEntity = this.websiteTemplateData.Get(requestDto.WebsiteTemplateId);
            var entity = requestDto.As<WebsiteTemplateAttachmentEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            var result = this.websiteTemplateAttachmentData.Add(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<WebsiteTemplateAttachmentDto>();
        }
        #endregion

        #region 修改网站模板附件
        public WebsiteTemplateAttachmentDto Update(WebsiteTemplateAttachmentPutDto putDto,AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteTemplateAttachmentId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            if (!this.websiteTemplateData.CheckWebsiteTemplateExists(putDto.WebsiteTemplateId))
            {
                throw new ArgumentException("此网站模板不存在!");
            }
            var entity = putDto.As<WebsiteTemplateAttachmentEntity>();

            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteTemplateAttachmentData.Update(entity).GetAwaiter().GetResult().As<WebsiteTemplateAttachmentDto>();
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 删除网站模板附件
        public bool Delete(int websiteTemplateAttachmentId)
        {
            if (!this.Exists(websiteTemplateAttachmentId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var oldDto = this.Get(websiteTemplateAttachmentId);
            var result = this.websiteTemplateAttachmentData.Delete(websiteTemplateAttachmentId).GetAwaiter().GetResult();
            string applicationRootDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).FullName;                         //当前系统根目录
            string websiteRootDirectory = Path.Combine(applicationRootDirectory, configuration["WebsiteName"]);                      //当前网站目录
            string defaultHost = configuration["DefaultHost"];
            var filePath = oldDto.FileAddress.Replace(defaultHost.TrimEnd('/'), websiteRootDirectory);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            this.redisService.DeleteMultipleKey(key, false);
            return result;
        }
        #endregion

        #region 获取网站模板附件
        public WebsiteTemplateAttachmentDto Get(int websiteTemplateAttachmentId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteTemplateAttachmentId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteTemplateAttachmentData.GetEntity(websiteTemplateAttachmentId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteTemplateAttachmentDto>();
        }
        #endregion

        #region 获取网站模板附件列表
        public IEnumerable<WebsiteTemplateAttachmentDto> GetList(int websiteTempalteId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteTempalteId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.websiteTemplateAttachmentData.GetList(websiteTempalteId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteTemplateAttachmentDto>>();
        }
        #endregion

        #region 获取网站模板附件分页
        public PageableList<WebsiteTemplateAttachmentDto> Query(int websiteTempalteId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, websiteTempalteId, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteTemplateAttachmentData.Query(websiteTempalteId,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteTemplateAttachmentDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteTemplateAttachmentDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}
